<?php
namespace app\common\traits;
/**
 * Created by PhpStorm.
 * User: HuangLuSen
 * Date: 2017/5/4
 * Time: 14:04
 */
use app\common\model\Order;
use think\Db;
use page\Page as Pages;
use app\common\model\Leibie;
trait Page{

    /**
     * 分页
     * @param $tablename
     * @param string $data
     * @param string $with
     * @param string $order
     * @param int $pagesize
     * @return false|\PDOStatement|string|\think\Collection
     * @internal
     */
    public function pageshow($tablename, $data='', $with='', $order='id desc', $pagesize=10)
    {
        $model = model($tablename);
        $count = $model->where($data)->with($with)->count();// 查询满足要求的总记录数
        $Page = new Pages($count,$pagesize);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');//定义分页类的的主题
        $show = $Page->newshow();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性

        $list = $model->where($data)->with($with)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('count',$count);
        $this->assign('page',$show);
        return $list;
    }

    /**  处理电话、微信 信息加密
     * @param $data
     * @return mixed
     * @internal
     */
    public function protectData($data){
        if($this->getBaomiqx() == 1){
            $condition = $this->getHidStates();//需要加密的状态
            if($this->hasRole('yyzg')){
                $data = doData($data,$condition,session('admin_user.adminid'),'yykf');
            }else{
                $data = doData($data,$condition);
            }
        }
        return $data;
    }


    /**
     * @return mixed
     * @internal
     */
    public function getOlistMoban(){
        if($this->hasRole(['qdjl','qdzg','qdzy'])){
            if(isMobile()){
                return $this->fetch('mobile/listing-qudao');
            }else{
                return $this->fetch('olist-qudao');
            }
        }elseif($this->hasRole(['yyjl','yyzg','yyzy'])){
            if(isMobile()){
                return $this->fetch('mobile/listing-yaoyue');
            }else{
                return $this->fetch('olist-yaoyue');
            }
        }elseif($this->hasRole(['mdjl','mdzg','mdzy'])){
            if(isMobile()){
                return $this->fetch('mobile/listing-mendian');
            }else{
                return $this->fetch('olist-huisuo');
            }
        }else{
            if(isMobile()){
                return $this->fetch('mobile/listing');
            }else{
                return $this->fetch('olist');
            }
        }

    }

    /**
     * @return mixed
     * @internal
     */
    public function getAddMoban(){
        if($this->hasRole('qdjl') || $this->hasRole('qdzg') || $this->hasRole('qdzy')){
            if(isMobile()){
                return $this->fetch('mobile/add_qudao');
            }else{
                return $this->fetch('add-qudao');
            }
        }elseif ($this->hasRole(['yyjl','yyzg','yyzy'])){
            if(isMobile()){
                return $this->fetch('mobile/add_yaoyue');
            }else{
                return $this->fetch('add-yaoyue');
            }
        /*}elseif ($this->hasRole(['mendianjingli','mednianzhuguan','mednianzhuanyuan'])){
            return $this->fetch('add-huisuo');*/
        }elseif ($this->hasRole(['zhuanjieshaojingli','zhuanjieshaozhuguan','zhuanjieshaozhuanyuan'])){
            if(isMobile()){
                return $this->fetch('mobile/add_zhuanjieshao');
            }else{
                return $this->fetch('add-zhuanjieshao');
            }
        }else{
            if(isMobile()){
                return $this->fetch('mobile/add_list');
            }else{
                return $this->fetch('add');
            }
        }
    }

    /**
     * @return mixed
     * @internal
     */
    public function getZjMoban(){
        if($this->hasRole('qudaojingli') || $this->hasRole('qudaozhuguan') || $this->hasRole('qudaozhuanyuan')){
            if(isMobile()){
                return $this->fetch('mobile/zhuijia_qudao');
            }else{
                return $this->fetch('zhuijia-qudao');
            }
        }elseif ($this->hasRole(['yaoyuejingli','yaoyuezhuguan','yaoyuezhuanyuan'])){
            if(isMobile()){
                return $this->fetch('mobile/zhuijia_yaoyue');
            }else{
                return $this->fetch('zhuijia-yaoyue');
            }
            /*}elseif ($this->hasRole(['mendianjingli','mednianzhuguan','mednianzhuanyuan'])){
                return $this->fetch('add-huisuo');*/
        /*}elseif ($this->hasRole(['zhuanjieshaojingli','zhuanjieshaozhuguan','zhuanjieshaozhuanyuan'])){
            if(isMobile()){
                //return $this->fetch('mobile/add_zhuanjieshao');
            }else{
                return $this->fetch('add-zhuanjieshao');
            }*/
        }else{
            if(isMobile()){
                return $this->fetch('mobile/zhuijia');
            }else{
                return $this->fetch('zhuijia');
            }
        }
    }

    /**
     * @return mixed
     * @internal
     */
    public function getEditMoban(){
        if($this->hasRole(['qdjl','qdzg','qdzy'])){
           if(isMobile()){
                return $this->fetch('mobile/edit-qudao');
            }else{
                return $this->fetch('edit-qudao');
            }
//        }elseif($this->hasRole(['zhuanjieshaojingli','zhuanjieshaozhuguan','zhuanjieshaozhuanyuan'])){
//            if(isMobile()){
//                return $this->fetch('mobile/edit-zhuanjieshao');
//            }else{
//                return $this->fetch('edit-zhuanjieshao');
//            }
        }elseif($this->hasRole(['yyjl','yyzg','yyzy'])){
            if($this->hasRole('yyzy')){ $this->assign('zhuanyuan',1); }else{ $this->assign('zhuanyuan',0);  }
            if(isMobile()){
                return $this->fetch('mobile/edit-yaoyue');
            }else{
                return $this->fetch('edit-yaoyue');
            }
        }elseif($this->hasRole(['mdjl','mdzg','mdzy'])) {
            if (isMobile()) {
                return $this->fetch('mobile/edit-mendian');
            } else {
                return $this->fetch('edit-huisuo');
            }
        }else{
            if(isMobile()){
                return $this->fetch('mobile/edit');
            }else{
                return $this->fetch('edit');
            }
        }
    }

}